<?php
namespace Admin\Model;
use Think\Model;

class ManagerModel extends Model{
	//自动验证
	protected $_validate = array(
		array('name','1,20','管理员帐号不得为空或大于20位！',self::EXISTS_VALIDATE,'length'),
		array('pass','require','管理员密码不得为空！',self::EXISTS_VALIDATE),
		array('repass','pass','两次输入的密码不相同！',self::EXISTS_VALIDATE,'confirm'),
		array('admin_mail','require','管理员邮箱不得为空！',self::EXISTS_VALIDATE),
	);
	
	//查询总记录
	public function total(){
		return $this->where($this->map())->count();
	}
	
	//查询所有
	public function all($firstRow){
		return $this->field('id,name,level,order_permit,qqau,date')
    				->where($this->map())
    				->order(array('date'=>'DESC'))
    				->limit($firstRow,C('PAGE_SIZE'))
    				->select();
	}
	
	//查询所有（不分页）
	public function all2(){
		$map = array();
    	$session = session(C('SESSION_KEY'));
    	if ($session['level']==2 && $session['order_permit']!=3) $map['id'] = $session['id'];
		return $this->field('id,name')
    				->where($map)
    				->order(array('date'=>'DESC'))
    				->select();
	}
	
	//登录
	public function login(){
		$data = array(
			'name'=>I('post.name'),
			'pass'=>I('post.pass')
		);
		if ($this->create($data)){
			$map = array(
				'name'=>I('post.name')
			);
			return $this->field('id,name,pass,level,permit,order_permit,qqau')->where($map)->find();
		}else{
			return $this->getError();
		}
	}
	
	//QQ登录
	public function qqlogin($qqau){
		$map['qqau'] = $qqau;
		return $this->field('id,name,level,permit,order_permit')->where($map)->find();
	}
	
	//绑定和解绑QQ
	public function qq($qqau,$id){
		$data = array(
			'id'=>$id ? $id : I('get.id'),
			'qqau'=>$qqau
		);
		return $this->save($data);
	}
	
	//查询一条
	public function one($id=0){
		$map['id'] = $id ? $id : I('get.id');
		return $this->field('name,level,permit,order_permit')->where($map)->find();
	}
	
	//添加
	public function insert(){
		$permit = I('post.permit');
		asort($permit);
		$data = array(
			'name'=>I('post.name'),
			'pass'=>I('post.pass'),
			'repass'=>I('post.repass'),
			'level'=>2,
			'permit'=>implode(',',$permit),
			'order_permit'=>I('post.order_permit'),
			'date'=>NOW_TIME
		);
		if ($this->create($data)){
			$data['pass'] = sha1(I('post.pass'));
			if ($this->repeat()) return '此帐号已存在！';
			$id = $this->add($data);
			return $id ? $id : 0;
		}else{
			return $this->getError();
		}
	}
	
	//添加超级管理员
	public function insert2(){
		$data = array(
			'name'=>I('post.admin_name'),
			'pass'=>I('post.admin_pass'),
			'repass'=>I('post.admin_repass'),
			'admin_mail'=>I('post.admin_mail'),
			'level'=>1,
			'date'=>NOW_TIME
		);
		if ($this->create($data)){
			$data['pass'] = sha1(I('post.admin_pass'));
			$id = $this->add($data);
			return $id ? $id : 0;
		}else{
			return $this->getError();
		}
	}
	
	//修改
	public function update(){
		$permit = I('post.permit');
		asort($permit);
		$data = array(
			'id'=>I('get.id'),
			'name'=>I('post.name'),
			'permit'=>implode(',',$permit),
			'order_permit'=>I('post.order_permit')
		);
		if (I('post.pass')){
			$data['pass'] = I('post.pass');
			$data['repass'] = I('post.repass');
		}
		if ($this->create($data)){
			if (I('post.pass')) $data['pass'] = sha1(I('post.pass'));
			if ($this->repeat(1)) return '此帐号已存在！';
			$id = $this->save($data);
			return $id ? $id : 0;
		}else{
			return $this->getError();
		}
	}
	
	//激活和取消激活
	public function activation($level){
		$data = array(
			'id'=>I('get.id'),
			'level'=>$level
		);
		return $this->save($data);
	}
	
	//删除
	public function remove(){
		$affected_rows = $this->delete(I('get.id'));
		if ($affected_rows) $this->execute('OPTIMIZE TABLE `__PREFIX__manager`');
		return $affected_rows;
	}
	
	//验证重复
	private function repeat($type=0){
		$map = array('name'=>I('post.name'));
		if ($type) $map['id'] = array('NEQ',I('get.id'));
		return $this->field('id')->where($map)->find();
	}
	
	//搜索
	private function map(){
		$map = array(
			'_complex'=>array(
				'name'=>array('like','%'.I('get.keyword').'%'),
				'_logic'=>'OR'
			)
		);
		return $map;
	}
}